package com.contextor.magiccards.web.handlers.auth.impl;

import com.contextor.magiccards.game.auth.GameAuthenticationManager;
import com.contextor.magiccards.model.errors.MagicCardsException;
import com.contextor.magiccards.model.user.User;
import com.contextor.magiccards.web.handlers.AbstractTOHandler;
import com.contextor.magiccards.web.handlers.auth.AuthToHandler;
import com.contextor.magiccards.web.to.user.UserProfileTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * User: sergey
 * Date: Mar 14, 2011 4:45:39 PM
 */
@Service
class AuthToHandlerImpl extends AbstractTOHandler implements AuthToHandler {
    @Autowired
    protected GameAuthenticationManager authManager;

    public UserProfileTO trackUser(String uid, String name) throws MagicCardsException {
        checkUid(uid);
        final User user = (authManager.isUserRegistered(uid)) ? authManager.authenticate(uid) : authManager.registerUser(uid);
        return new UserProfileTO(user);
    }
}
